package com.hsmpay.mobile.action.terminal;

import com.hsmpay.common.util.DateConvertUtil;
import com.hsmpay.common.util.system.Constant;
import com.hsmpay.mobile.action.base.MobileBaseAction;
import com.hsmpay.mobile.pojo.terminal.TerminalErrorLog;
import com.hsmpay.mobile.service.terminal.TerminalErrorLogService;
import com.hsmpay.mobile.util.ImgFileUpload;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 错误日志 action
 *
 * @author 颜铃璋
 * @date 2013-1-18
 */
@Controller("errorLogAction")
@Scope("prototype")
public class ErrorLogAction extends MobileBaseAction {
    private static final long serialVersionUID = 4685508381751184994L;
    private static Logger log = Logger.getLogger(ErrorLogAction.class);

    @Resource(name = "terminalErrorLogService")
    private TerminalErrorLogService<TerminalErrorLog, Long> terminalErrorLogService;

    private String path;//用户反馈信息
    private String errorLog;//错误日志 二进制流

    /**
     * 上传错误日志文件
     *
     * @return
     * @throws Exception
     */
    public String uploadErrorLog() throws Exception {
        log.debug("--- searchBankList() start ---");
        JSONObject js = JSONObject.fromObject(getRequest().getAttribute(FC_DATA));
        terminalTypeId = Long.parseLong(js.getString("terminalTypeId"));
        roleId = Long.parseLong(js.getString("roleId"));
        clientType = Integer.parseInt(js.getString("clientType"));
        if (js.containsKey("userId"))
            userId = Long.parseLong(js.getString("userId"));
        if (js.containsKey("errorLog")) {
            errorLog = js.getString("errorLog");
            /*String relPath = getRealPath("/document")+File.separator+"temp"+File.separator+DateConvertUtil.dateString(new Date(),"yyyyMMddhhmmssSSS")+".text";
			errorLog = ImgFileUpload.getPicByBase64Str(relPath,errorLogStr);*/
        }

        log.debug("terminalTypeId = " + terminalTypeId);
        log.debug("roleId = " + roleId);
        log.debug("clientType = " + clientType);
        log.debug("userId = " + userId);
        //log.debug("errorLogStr = "+errorLogStr);
        //log.debug("errorLogContentType = "+errorLogContentType);
        if (null == terminalTypeId) {
            log.warn("上传错误日志文件时, 传入参数terminalTypeId为空");
            sendAppErrorMsg("6001", "terminalTypeId");
            return null;
        }
        if (null == roleId) {
            log.warn("上传错误日志文件时, 传入参数roleId为空");
            sendAjaxResponse("0");
            return null;
        }
        if (null == clientType) {
            log.warn("上传错误日志文件时, 传入参数clientType为空");
            sendAppErrorMsg("6001", "clientType");
            return null;
        }
        if (null == userId) {
            log.warn("上传错误日志文件时, 传入参数userId为空");
            sendAppErrorMsg("6001", "userId");
            return null;
        }
        if (StringUtils.isBlank(errorLog)) {
            log.warn("上传错误日志文件时, 传入参数errorLog为空");
            sendAppErrorMsg("6001", "errorLog");
            return null;
        }
        try {
            //上传错误日志文件
            Date date = new Date();
            String dateStr = DateConvertUtil.dateString(date, "yyyyMMddhhmmssSSS");

            String errorLogUrl = Constant.getAttribute("SERVER_ERRORLOG_URL");
            StringBuilder sb = new StringBuilder();
            String filePath = getRealPath(errorLogUrl);
            String fileType = ".text";//ImgFileUpload.getFileType(errorLogFileName);
            filePath = ImgFileUpload.mkDirectory(sb.append(filePath).append(File.separator).append(userId).toString());
            filePath = sb.append(File.separator).append(dateStr).append(fileType).toString();

            sb = new StringBuilder();
            path = sb.append(errorLogUrl).append(userId).append("/").append(dateStr).append(fileType).toString();
            ImgFileUpload.uploadFileBase64Str(filePath, errorLog);

            TerminalErrorLog terminalErrorLog = new TerminalErrorLog();
            terminalErrorLog.setTerminalTypeId(terminalTypeId);
            terminalErrorLog.setRoleId(roleId);
            terminalErrorLog.setClientType(clientType);
            terminalErrorLog.setUserId(userId);
            terminalErrorLog.setPath(path);

            long flag = terminalErrorLogService.insertEntity(terminalErrorLog);
            Map<String, Object> result = new LinkedHashMap<String, Object>();
            if (flag > 0) {
                log.debug("上传错误日志文件成功");
                result.put("reqCode", "0000");
                result.put("reqMsg", "上传错误日志文件成功！");
                sendAppMsg(result);
            } else {
                log.warn("上传错误日志文件数据库处理失败！");
                sendAppErrorMsg("9999");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            //errorLog.delete();
            log.debug("--- searchBankList() end ---");
        }
        return null;
    }


    //---------------------set get start--------------------
    //---------------------set get end----------------------
}
